<?php

require_once('BaseModel.php');

class Package_model extends BaseModel {

    function Package_model()
    {
        $publicdata = array(
			'name',
			'website',
			'resp_usr',
			'licence',
			'category',
			'short_desc',
			'last_update',
			'status');
        parent::BaseModel('cr_packages', 'pkg_id', $publicdata);
    }    
    
    /**
     * Loads a package based on his name
     * Returns true on success, false otherwise
     */
    function load_package_from_name($name) {
    	$this->db->where('name', $name);
		return $this->load();
    }
        
    /**
     * Loads a package based on his id
     * Returns true on success, false otherwise
     */
    function load_package_from_id($id) {
    	$this->db->where('pkg_id', $id);
		return $this->load();
    }
    
    /**
     * Creates a new package using its infos
     * Returns  true if the package was successfully created
     * 			an array mapping error messages onto bad fields if it failed 
     */
    function new_package($infos) {
		$checkeddata = array();
		$checkeddata['creation_date'] = date('Y-m-d');
    	
		//checks whether this nickname is already in use
		if ($this->exists(array('name' => $infos['name'])))
    		return array('name' => 'This name is already used');
    	
    	$result = parent::new_data($infos, $checkeddata);
    	if (is_array($result))
    		return $result;

        //reloading the package from his name, thus getting his id and complementary infos
        $this->load_package_from_name($this->data['name']);
        return true;
    }
    
	/**
	 * Stores one package's information in the database
	 * Returns true if the infos got saved, false if an error occured
	 */
	function save_package($pkgid, $data) {
		return $this->save_data($pkgid, $data);
	}
    
	/**
	 * Checks the validity of the infos stored in the $this->data array
	 * Returns true if each field is valid, false otherwise
	 */
	public function fields_are_valid($data) {
		$errormet = false;
		$errors = array();
		foreach ($data as $key => $value) {
			if (isset($value) && strlen($value) == 0) {
				$errors[$key] = $key.' cannot be empty';
				$errormet = true;
			}
		}
		if (isset($data['name']) && strlen($data['name']) > 50) {
			$errors['name'] = 'Name is too long';
			$errormet = true;
		}
		if (isset($data['licence']) && strlen($data['licence']) > 30) {
			$errors['licence'] = 'Licence is too long';
			$errormet = true;
		}
		if (isset($data['category']) && strlen($data['category']) > 30) {
			$errors['category'] = 'Category is too long';
			$errormet = true;
		}
		if (isset($data['website']) && strlen($data['website']) > 150) {
			$errors['website'] = 'Website URL is too long';
			$errormet = true;
		}
		if (isset($data['status']) && !in_array($data['status'], $this->statuses)) {
			$errors['status'] = 'Status is invalid';
			$errormet = true;
		}
		if ($errormet)
			return $errors;
		return true;
	}
	
	/**
	 * Returns an array of $count packages starting with the $first
	 * If $count is null, returns all values
	 * This array is sorted on the $sorted column
	 * $match is an array containg a key and a value which is to match.
	 * ie : $match = array ($field => $namematch)
	 */
	function get_packages($first = null, $count = null,  $match = null, $sorted = 'name') {
		return $this->get_data($first, $count, $sorted, $match);
	}
    
    /**
     * Returns an array containing all known package categories
     */
    function get_categories() {
    	$query = $this->db->query('SELECT DISTINCT category FROM cr_packages ORDER BY category ASC');
    	$result = array();
		foreach ($query->result_array() as $value)
			array_push($result, $value['category']);
		return $result;
    }
    
    /**
     * Returns the number of packages
	 * $match is an array containg a key and a value which is to match.
	 * ie : $match = array ($field => $namematch)
	 */
	function get_pkg_count($match = null) {
		return $this->get_data_count($match);
	}
	
	/**
	 * Returns whether the user $user_id is responsible for this package
	 */
	function is_responsible($user_id, $pkg_id) {
		$this->db->select('resp_usr');
		$this->db->where('pkg_id', $pkg_id);
		$query = $this->db->get('cr_packages');
		$array = $query->row_array();
		return $user_id == $array['resp_usr'];
	}
}

?>
